package practice_05;

import java.time.LocalDateTime;
import java.util.Scanner;

public class ToBinary {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入一个数转化为二进制: ");
        int x = sc.nextInt();
        System.out.println("转化后的二进制:" + To_Binary(x));
        System.out.println("自带方法:" + Integer.toString(x, 2));
    }

    //将整数转为二进制
    public static String To_Binary(int x) {
        StringBuilder sb = new StringBuilder();
        while (x != 0) {
            int y = x % 2;
            sb.insert(0, y);
            x /= 2;
        }
        return sb.toString();
    }
}
